@extends("ogreencard/layout/layout")
@section("h1","用户管理")
@section("h2","添加、编辑用户")
@section("content")
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form" action="./addUser">
            {{csrf_field()}}
            <input type="hidden" name="id" value="{{$user->id}}">

            <div class="layui-form-item">
                <label for="L_username" class="layui-form-label"><span class="x-red">*</span>登录名称</label>
                <div class="layui-input-inline">
                    <input  name="name" required lay-verify="name" autocomplete="off" class="layui-input" value="{{$user->name}}">
                </div>
            </div>

            <div class="layui-form-item">
                <label for="L_username" class="layui-form-label"><span class="x-red">*</span>登录密码</label>
                <div class="layui-input-inline">
                    <input  name="pwd"  lay-verify="pwd" autocomplete="off" class="layui-input" value="">
                    <input type="hidden" value="{{$user->pwd}}" id="oldpwd">
                </div>
            </div>

            <div class="layui-form-item">
                <label for="L_username" class="layui-form-label"><span class="x-red">*</span>真实姓名</label>
                <div class="layui-input-inline">
                    <input  name="real_name" required lay-verify="realname" autocomplete="off" class="layui-input" value="{{$user->real_name}}">
                </div>
            </div>

            <div class="layui-form-item">
                <label for="L_username" class="layui-form-label"><span class="x-red">*</span>用户角色</label>
                <div class="layui-input-inline">
                    <select name="role" class="layui-select"  lay-verify="role" value="{{$user->role}}" required>
                        <option value="">请选择</option>
                        @foreach($roles as $r)
                            <option value="{{$r->role_id}}" @if($user->role == $r->role_id) selected @endif >{{$r->role_name}}</option>
                        @endforeach
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label for="L_repass" class="layui-form-label"></label>
                <button class="layui-btn" lay-filter="add" lay-submit="">增加</button>
            </div>
        </form>
    </div>
</div>
<script>
    layui.use(['form', 'layer'],
        function () {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer;

            //自定义验证规则
            form.verify({
                name: function (value) {
                    if (value.length < 1) {
                        return '登录名称不能为空';
                    }
                },
                pwd:function(value){

                    var pwd1 = $('#oldpwd').val();

                    if(pwd1 == "" && value == ""){
                        return "密码不能为空";
                    }
                },
                realname:function(value){
                    if(value.length<1){
                        return "真实名称不能为空";
                    }
                },
                role:function(value){
                    if(value == ''){
                        return "请选择角色";
                    }
                }
            });

            //监听提交
            form.on('submit(add)', function (data)
            {
                console.log(data);
                //发异步，把数据提交给php
                layui.jquery.post({
                    url:"./addUser",
                    data:data.field,
                    success:function(res){
                        layer.alert("操作成功",{icon:6},function(){

                            parent.window.location.reload();

                            // 获得frame索引
                            //var index = parent.layer.getFrameIndex(window.name);

                            //关闭当前frame
                            //parent.layer.close(index);
                        })
                    }
                });
                return false;
            });

        });
</script>
@endsection
